/**
 * Industry数据传输对象
 *
 * @author sxf
 * @email sxf02615@163.com
 * @date 2025/1/15
 */
package com.sxf.crm.dto;


import com.sxf.crm.entity.Industry;
import lombok.Data;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 行业数据传输对象
 */
@Data
public class IndustryDTO {
    private Long id;
    private String name;
    private String path;
    private Long parentId;
    private String parentName;
    private List<IndustryDTO> children;
    private Long createUserId;
    private String creatorName;
    private Long createAt;
    private Long updateUserId;
    private String updaterName;
    private Long updateAt;

    public static IndustryDTO fromEntity(Industry industry) {
        if (industry == null) {
            return null;
        }

        IndustryDTO dto = new IndustryDTO();
        dto.setId(industry.getId());
        dto.setName(industry.getName());
        dto.setPath(industry.getPath());

        if (industry.getParent() != null) {
            dto.setParentId(industry.getParent().getId());
            dto.setParentName(industry.getParent().getName());
        }

        if (industry.getChildren() != null && !industry.getChildren().isEmpty()) {
            dto.setChildren(industry.getChildren().stream()
                    .map(IndustryDTO::fromEntity)
                    .collect(Collectors.toList()));
        }

        dto.setCreateUserId(industry.getCreateUserId());
        dto.setCreateAt(industry.getCreateAt());
        dto.setUpdateUserId(industry.getUpdateUserId());
        dto.setUpdateAt(industry.getUpdateAt());

        return dto;
    }
}
